<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../common/taglibs.jsp"%>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>用户注册</title>
<link rel="stylesheet" type="text/css"
	href="${ctx }/resource/css/reset.css">
<link rel="stylesheet" type="text/css"
	href="${ctx }/resource/css/enroll.css">
<script type="text/javascript" src="${ctx }/resource/js/lib/rem.js"></script>
</head>
<html lang="en">
<body class="login_bg">
	<div class="pos-per">
		<div class="reg_con">
			<input type="text" id="phone" placeholder="请输入手机号码" /> 
			<input type="text" id="code" placeholder="请输入验证码" />
			<button id="btn_verification">获取验证码</button>
		</div>
		<a href="javaScript:;" onclick="toSaveUser();" class="submit">注册</a>
	</div>
	<div class="empty_div"></div>
	<script type="text/javascript"
		src="${ctx }/resource/js/lib/jquery-3.3.1.js"></script>
	<script type="text/javascript">
			var smsId=0;
			//   验证码倒计时
		   	var wait = 120; 
			get_code_time = function (o) { 
		    	if(wait == 0) {
		    		timer = 120;
		    		o.removeAttribute("disabled");
		    		o.style.backgroundColor="#f9fde5";
		    		o.innerHTML = "获取验证码";
		    		return false;
		    	} else {
		    		o.attr('disabled',"true");
		    		o.css('background-color',"#a9ab9b");
		    		o.html("还剩" + wait + "秒");
					wait--; 
					setTimeout(function() { 
						get_code_time(o);
					}, 1000);
		    	}
		    };
		    $("#btn_verification").click(function () {
				var o = $(this); 
				if(checkSubmitMobil()){
				get_code_time(o); 
				$.ajax({
					type: "POST",
					url: "getVerifyCode",
					data: {
						phone : $("#phone").val(),
					},
					dataType:'json',
					success: function(data){
						if(data.result == '00'){ 
								smsId=data.id;
								alert("验证码已发送至您的手机"); 
							} else if(data.result=='-1'){
								alert("短信已下发至您手机，请2分钟后再试!");
							} else if(data.result=='-2'){
								alert("请先填写资料注册会员!");
							}  else{ 
								alert("短信验证码发送失败"); 
							} 
						}, 
					error: function (data) {
						alert("短信验证码发送失败"); 
					} 
				});
			}});
		    function checkSubmitMobil() { 
				if ($("#phone").val() == "") { 
					alert("手机号码不能为空！"); 
					$("#phone").focus(); 
					return false; 
				} 
				if (!$("#phone").val().match(/^0?(13[0-9]|14[57]|15[012356789]|17[013678]|18[0123456789]|19[0123456789])[0-9]{8}$/)) { 
					alert("手机号码格式不正确！"); 
					$("#phone").focus(); 
					return false; 
				} 
				return true; 
			}
		    
		    
		  //提交用户信息
			function toSaveUser(){
				var code=$("#code").val();
				if(code==''){
					alert("填写验证码鸭!");
					return;
				}
				$.ajax({
					type: "POST",
					url: "saveUserPhone",
					data: {
						phone:$("#phone").val(),
						code:$("#code").val(),
						openId:'${openId}'
					},
					dataType:'json',
					success: function(data){
						switch(data.code){
						  case ("-1"):
							  alert("验证码错误！");
							  break;
						  case ("-2"):
							  alert("验证码超时，请重新获取!");
							  break;
						  case ("1"):
							  window.location.href="${ctx}/vipCentent/oauth?type=toVipCentent";
							  break;
						}
					}, 
					error: function (data) {
						alert("数据处理异常！"); 
					} 
				});
			}
		</script>
</body>
</html>